// pages/task/index.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    selectedIndex: 0,
    page: 1,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    Object.assign(this, ImgListCell);
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    /**
     * 显示 loading
     */
    that.setData({
      loadinghidden: false
    });
    that.doLoadData();
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成
    // 页面渲染完成
    var that = this;

    // 数据加载完成后 延迟隐藏loading
    setTimeout(function() {
      that.setData({
        loadinghidden: true
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //添加选中效果
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      var tabBar = wx.getStorageSync("tabBar");
      this.getTabBar().setData({
        selected: 0, //这个数是，tabBar从左到右的下标，从0开始
        list: tabBar
      })
    }
    //获取用户信息
    http.ApiGet(apiUrl.UrlUserInfo, {}, function(res) {
      if (res.code == 1) {
        var user = res.data.userinfo;
        wx.setStorageSync("user", user);
        var tabBar = util.setTabBarData(res.data);

        if (typeof that.getTabBar === 'function' &&
          that.getTabBar()) {
          that.getTabBar().setData({
            list: tabBar
          })
        }
      } else {
        wx.reLaunch({
          url: '../login/index'
        })
      }
    });
  },
  topNavTap: function(e) {
    var that = this;
    const data = e.currentTarget.dataset;
    var selectedIndex = data.index;
    that.setData({
      selectedIndex: selectedIndex
    });
    that.doLoadData();
  },
  doLoadData: function() {
    var that = this;
    var parameter = {};
    parameter['status'] = that.data.selectedIndex;
    parameter['page'] = 1;

    http.ApiGet(apiUrl.UrlTaskList, parameter, function(res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          newData = newData.map(function(item) {
            item.ptime = item.create_time_text; // util.getDateDiff(item.ptime + '000');
            // if (item.user_name != null) {
            //   item.user_name = item.user_name.substring(0, 8);
            // } else {
            //   item.user_name = "";
            // }
            item.autor = "";
            if (item.files.length > 0) {
              item.thumb = item.files;
            }
            if (item.isread == 0) {
              item.label = "未读";
            }

            return item;
          })
        } else {
          newData = [];
          // wx.showModal({
          //   title: "暂无数据！"
          // });
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: newData
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.doLoadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['status'] = that.data.selectedIndex;
    parameter['page'] = page;
 
    http.ApiGet(apiUrl.UrlTaskList, parameter, function(res) {

      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {

          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function(item) {
              item.ptime = item.create_time_text;
              item.autor = "";
              if (item.files.length > 0) {
                item.thumb = item.files;
              }
              if (item.isread == 0) {
                item.label = "未读";
              }

              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  listCellTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
    wx.navigateTo({
      url: '../task/detail?id=' + item.id
    })
  },
})